#!/bin/bash

DEBUG=${ENTRYPOINT_DEBUG:false}

print_banner(){
   printf '# Copyright 2004-2017, ZTESoft, Inc. All rights reserved.\n'
   printf '# http://gitlab.ztesoft.com/zcm/zcm-cmdb\n'
   printf '# Enabled features:[centos,java]\n'
   printf "# Run 'docker run $IMAGE_NAME help' for more information of this image\n\n"
}

print_help(){
   printf '\n'
   printf 'Usage:\n'
   printf '\tdefault launch zcm resource application\n'
   printf '\thelp\tPrint help information\n'
   printf '\tbash\tEnter bash command line\n'
   printf '\n\n'
   print_banner

}

print_console()
{
    printf "[`date +%Y%m%d%H%M%S`][$1]=> $2\n"
}

print_debug(){
    if [ ${DEBUG} ]
    then
        print_console "DEBUG " "$1"
    fi
}

print_log(){
    print_console "NORMAL" "$1"
}

watch_loop(){
    LOOP_SECS=5
    RETRY_SEC=3
    MAX_ERR=4
    ERR_CNT=0
    print_debug "enter watch loop"
    while [ true ]
    do
        main status
        if [ $? = 0 ]
        then
           ERR_CNT=0
           sleep $LOOP_SECS
        else
           ERR_CNT=`expr $ERR_CNT + 1`
           print_log "app abnormal, retry $ERR_CNT"
           if [ $ERR_CNT -ge $MAX_ERR ]
           then
              print_log "app abnormal, quit"
              break;
           fi
           sleep $RETRY_SEC
         fi
    done
}

query_pid(){
    PID=`ps -ef | grep "$1" | grep -v grep | awk '{print $2}' | tr "\n" " "`
    echo $PID
}

proc_check(){
    PID=$(query_pid "$1")
    print_debug "query process:[$1], pid:[$PID]"
    if [ "$PID" = "" ]
    then
        return 1
    else
        return 0
    fi
}

run_app(){
    # use exec and gosu to trap unix signal
    # exec gosu user "$@" &
    exec $1 &
    print_log "app [$1] started, pid:[$!]"
    return $?
}

stop_app(){
    PID=$(query_pid "$1")
    print_log "kill app [$1], pid:[$PID]"
    kill $PID
    return $?
}

main(){
    case $1 in
        run)
            run_app "java -DAPP_NAME=zcmCmdb -Dspring.config.location=/root/conf/application.properties -jar /root/lib/*.jar"
        ;;

        stop)
            stop_app "java -DAPP_NAME=zcmCmdb"
        ;;

        status)
            proc_check "java -DAPP_NAME=zcmCmdb"
            return $?
        ;;

        start)
            main run
            watch_loop
        ;;

        restart)
            main stop
            main run
        ;;

        liveness)
            resp_code=$(curl -s --head --max-time 5 -o /dev/null -w '%{response_code}' http://10.45.80.26/zcm-cmdb/cluster/k8s/state/1)
            print_debug "liveness response_code:[$resp_code]"
            if [ $resp_code = "200" ]
            then
                return 0
            else
                return 1
            fi
        ;;

        readiness)
            resp_code=$(curl -s --head --max-time 5 -o /dev/null -w '%{response_code}' http://10.45.80.26/zcm-cmdb/cluster/k8s/state/1)
            print_debug "readiness response_code:[$resp_code]"
            if [ $resp_code = "200" ]
            then
                return 0
            else
                return 1
            fi
        ;;

        debug)
            DEBUG=true
            main status
            print_debug "status:[$?]"
            main liveness
            print_debug "liveness check:[$?]"
            main readiness
            print_debug "readiness check:[$?]"
        ;;

        help | h | -h)
            print_help
        ;;

        *)
           print_banner
           print_log "default: exec $@"
           exec "$@"
        ;;
    esac
}

main $@